package com.atguigu.lease.web.admin.task;


import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Component
public class ScheduledTasks {

    @Autowired
    private LeaseAgreementService leaseAgreementService;

    //每天的0点执行
    //                 秒 分钟 小时 天 月 星期
    @Scheduled(cron = "0 0 0 * * ?")
    @Transactional
    public void a(){
        //获取当前系统时间
        Date now = new Date();

        // 扫描所有已签约或者退租待确认的租约

        QueryWrapper<LeaseAgreement> leaseAgreementQueryWrapper = new QueryWrapper<>();
        leaseAgreementQueryWrapper.in("status", LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);


        List<LeaseAgreement> leaseAgreements = leaseAgreementService.list(leaseAgreementQueryWrapper);
        for (LeaseAgreement leaseAgreement : leaseAgreements) {
            if(leaseAgreement.getLeaseEndDate().before(now)){
                leaseAgreement.setStatus(LeaseStatus.EXPIRED);
                leaseAgreementService.updateById(leaseAgreement);
            }
        }
    }


}
